﻿/*----------------------------------------------------------------
//  <copyright file="SmsController.cs" company="MicroCloud@151504200868">
//      Copyright © 2020-2024 MicroCloud Corporation, All rights reserved.
//  </copyright>
//  <site>https://gitee.com/chenmm123/microclouds</site>
//  <last-editor>cmm</last-editor>
//  <last-date>2023-09-13 14:27</last-date>
//----------------------------------------------------------------*/

namespace MicroCloud.Api.SMS.Controllers
{
    #region "短信"
    /// <summary>
    /// 短信
    /// </summary>
    [Description("短信")]
    //[ModuleInfo(Code = "Sms", Name = "短信", OrderNo = 1)]
    public class SmsController : SmsControllerBase
    {
        #region "属性"
        #region "获取 短信验证码服务"
        /// <summary>
        /// 获取 短信验证码服务
        /// </summary>
        protected ISmsVerifyCodeService SmsVerifyCodeService => ServiceProvider.GetService<ISmsVerifyCodeService>();
        #endregion

        #endregion

        #region "Action方法"
        #region "发送短信验证码"
        /// <summary>
        /// 发送短信验证码
        /// </summary>
        /// <param name="dto">手机号码DTO</param>
        /// <returns></returns>
        [HttpPost]
        [UnitOfWork]
        [NonFunction]
        [Description("发送验证码")]
        public virtual async Task<AjaxResult> SendVerifyCode(PhoneDto dto)
        {
            Check.NotNull(dto, nameof(dto));
            if (!ModelState.IsValid)
            {
                return AjaxResult.Error(I18N.T("提交信息验证失败"));
            }
            //if (!_verifyCodeService.CheckCode(dto.VerifyCode, dto.BizId))
            //{
            //    return new AjaxResult("验证码错误，请刷新重试", AjaxResultType.Error);
            //}

            var requestIp = HttpContext.GetClientIp();

            var operatResult = await SmsVerifyCodeService.SendAsync(6, ValidateCodeType.Number, requestIp, dto.PhoneNumber);

            return operatResult.ToAjaxResult(true);
        }
        #endregion
        #region "校验短信验证码"
        /// <summary>
        /// 验证短信验证码的有效性
        /// <para>只作为前端Ajax验证，验证成功不确认验证码，验证码仍需传到后端进行再次验证</para>
        /// </summary>
        /// <param name="dto">短信验证码校验DTO</param>
        /// <returns>是否有效</returns>
        [HttpPost]
        [UnitOfWork]
        [NonFunction]
        [Description("校验验证码")]
        public virtual async Task<bool> CheckVerifyCode(VerifyCodeCheckDto dto)
        {
            Check.NotNull(dto, nameof(dto));
            if (dto.CodeId.IsMissing() || dto.Code.IsMissing() || dto.PhoneNumber.IsMissing())
            {
                return true;
            }
            var operatResult = await SmsVerifyCodeService.CheckAsync(dto, false);
            return operatResult.Succeeded;
        }
        #endregion
        #region "确认短信验证码"
        /// <summary>
        /// 确认短信验证码
        /// </summary>
        /// <param name="dto">短信验证码确认DTO</param>
        /// <returns></returns>
        [HttpPost]
        [UnitOfWork]
        [NonFunction]
        [Description("确认验证码")]
        public virtual async Task<AjaxResult> ConfirmVerifyCode(VerifyCodeConfirmDto dto)
        {
            Check.NotNull(dto, nameof(dto));
            if (dto.CodeId.IsMissing() || dto.Code.IsMissing() || dto.PhoneNumber.IsMissing())
            {
                return AjaxResult.Error(I18N.T("没有传递有效的验证参数"));
            }
            var operatResult = await SmsVerifyCodeService.CheckAsync(dto, dto.ConfirmIfSuccess);
            return operatResult.ToAjaxResult(true);
        }
        #endregion

        #endregion

    }
    #endregion

}
